<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\ProductRequest;
use App\Model\Admin\CategoriesModel;
use Illuminate\Http\Request;
use App\Model\Admin\ProductModel;

class ProductController extends Controller
{

    /**
     * 字段
     *
     * @var array
     */
    protected $form_name = [
        'title','keywords','description','product_name','product_content','url','product_banner','product_img','product_abstract','category_id'
    ];

    /**
     * 列表页
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        return view('admin.product.index');
    }

    /**
     * 添加页面
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        $data['product_children'] = CategoriesModel::where('pid', 1)->get();
        $data['edit'] = false;
        return view('admin.product.add')->with('data',$data);
    }

    /**
     * 保存
     *
     * @param ProductRequest $request
     * @return array
     */
    public function store(ProductRequest $request)
    {
        $data = $request->only($this->form_name);
        $Product = new ProductModel();
        foreach ($this->form_name as $v){
            $Product -> $v = $data[ $v ];
        }
        if ( $Product -> save() ){
            return $this->res_msg(0, '成功');
        }else {
            return $this->res_msg(1, '失败');
        }
    }

    /**
     * 修改
     *
     * @param $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id)
    {
        $data['product_children'] = CategoriesModel::where('pid', 1)->get();
        $data['edit'] = true;
        $data['info']  = ProductModel::where('id',$id)->first();
        $data['info']->banners = explode(',',$data['info']->product_banner);
        return view('admin.product.add')->with('data',$data);
    }

    /**
     * 修改
     *
     * @param ProductRequest $request
     * @param $id
     * @return array
     */
    public function update(ProductRequest $request, $id)
    {
        $Product = new ProductModel();
        $product = $Product->findOrFail($id);
        foreach ($this->form_name as $v){
            $product -> $v = $request->get($v);
        }
        if ($product->save()){
            return $this->res_msg(0, '修改成功');
        } else {
            return $this->res_msg(1, '修改失败');
        }

    }

    /**
     * 删除
     *
     * @param $id
     * @return array
     * @throws \Exception
     */
    public function destroy($id)
    {
        $Product = new ProductModel();
        $product = $Product->findOrFail($id);
        if ($product->delete()){
            return $this->res_msg(0, '删除成功');
        } else {
            return $this->res_msg(1, '删除失败');
        }
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function getProductList(Request $request){
        $page = $request->page ? : 1;
        $limit = $request ->limit ? : 10;
        $category_id = $request ->category_id ? : 0;
        if ($category_id == 0){
            $where = [
                ['products.id', '>', 0]
            ];
        }else{
            $where = [
                ['products.category_id','=', $category_id]
            ];
        }
        $data['code'] = 0;
        $data['msg'] = '成功';

        $Product = new ProductModel();
        $data['count'] = $Product -> count();
        $data['data'] = $Product
            ->leftjoin('categories','products.category_id','=','categories.id')
            ->select(['categories.id as category_id','categories.name as category_name', 'products.id',
                'products.product_name','products.order','products.is_show','products.created_at'])
            ->where($where)
            ->skip($limit * ($page -1))
            ->take($limit)
            -> get();

        return $data;
    }
}
